//==========================================================
// 此代码由TilverNET代码生成工具自动生成，请不要修改
// 代码生成器版本号：1.2
// 作者：landy
// 联系方式：(QQ)86566178
// 创建时间：2014/6/21 10:58:22
//==========================================================

using System;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Linq;
using Tilver.Data;
using Tilver.Data.DataPager;

namespace Weixin.MP.DAL.WeixinMP
{
	#region EO

	/// <summary>
	/// 系统参数配置 《dbo.SysConfig(实体类)》
	/// </summary>
	[Serializable]
	public class SysConfigEO : IRowMapper<SysConfigEO>
	{
		protected bool _hasOriginal = false;
		protected string _sysKey;
		protected string _originalSysKey;
		protected string _sysValue;
		protected SysConfigEO.SysTypeEnum _sysType;

		/// <summary>
		/// 主键是否有原始值（即数据库中保存的值），用于实体对象更新时
		/// </summary>
		public bool HasOriginal
		{
			get { return _hasOriginal; }
		}


		/// <summary>
		/// 参数名称 《[主键]  [varchar]》
		/// </summary>
		public string SysKey
		{
			get { return _sysKey; }
			set { _sysKey = value;}
		}

		/// <summary>
		/// 参数名称 《数据库中的原始主键值副本》
		/// </summary>
		public string OriginalSysKey
		{
			get { return _originalSysKey; }
			set { 
				_hasOriginal = true;
				_originalSysKey = value;
			}
		}

		/// <summary>
		/// 参数值 《[字段]  [nvarchar]》
		/// </summary>
		public string SysValue
		{
			get { return _sysValue; }
			set { _sysValue = value;}
		}

		/// <summary>
		/// 参数类型 《[字段]  [tinyint]》
		/// </summary>
		/// <remarks>
		///    Sys-1-系统参数
		/// </remarks>
		public SysConfigEO.SysTypeEnum SysType
		{
			get { return _sysType; }
			set { _sysType = value;}
		}

		/// <summary>
		/// 默认构造函数
		/// </summary>
		public SysConfigEO() 
		{
			SysKey = string.Empty;
		}

		/// <summary>
		/// 自定义构造函数
		/// </summary>
		/// <param name = "sysKey">参数名称</param>
		/// <param name = "sysValue">参数值</param>
		/// <param name = "sysType">
		/// 参数类型
		///    Sys-1-系统参数
		/// </param>
		public SysConfigEO(string sysKey, string sysValue, SysConfigEO.SysTypeEnum sysType) 
		{
			_sysKey = sysKey;
			_sysValue = sysValue;
			_sysType = sysType;
		}


		/// <summary>
		/// 将结果集映射成实体对象
		/// </summary>
		/// <param name = "reader">只进结果集流</param>
		/// <return></return>
		public SysConfigEO MapRow(IDataReader reader)
		{
			var ret = new SysConfigEO();
			ret.SysKey = (string)reader["SysKey"];
			ret.OriginalSysKey = ret.SysKey;
			ret.SysValue = (reader["SysValue"] is DBNull) ? null : (string)reader["SysValue"];
			ret.SysType = (SysConfigEO.SysTypeEnum)reader["SysType"];
			return ret;
		}

		/// <summary>
		/// 将结果集映射成实体对象，返回映射方法
		/// </summary>
		/// <return></return>
		public Func<IDataReader, SysConfigEO> GetMapper()
		{
			return MapRow;
		}


		/// <summary>
		/// 参数类型
		/// </summary>
		/// <remarks>
		///    Sys-1-系统参数
		/// </remarks>
		public enum SysTypeEnum : byte
		{
			/// <summary>
			/// 系统参数
			/// </summary>
			Sys = 1,

		}

	}


	#endregion
	#region MO

	/// <summary>
	/// 系统参数配置 《dbo.SysConfig(逻辑类)》
	/// </summary>
	public class SysConfigMO : DbManagerBase
	{

		#region 构造函数
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name = "routeData">路由数据</param>
		public SysConfigMO(object routeData = null) : base(routeData) { }

		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name = "connectionStringName">连接字符串名称</param>
		public SysConfigMO(string connectionStringName) : base(connectionStringName) { }

		#endregion

		#region 私有方法
		/// <summary>
		/// 数据库访问操作方法
		/// </summary>
		/// <param name = "sql">SQL语句</param>
		/// <return>返回数据操作对象</return>
		private DbAdoBase AdoBase(string sql)
		{
			return DataBase.CreateDbAdo(sql, CommandType.Text);
		}

		#endregion

		#region Select方法集合
		/// <summary>
		/// 按主键查询
		/// </summary>
		/// <param name = "sysKey">参数名称</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public SysConfigEO SelectByPK(string sysKey, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			sql.AppendFormat(@"SELECT [SysKey], [SysValue], [SysType] FROM [dbo].[SysConfig] WHERE [SysKey]=@SysKey ");
			return AdoBase(sql.ToString())
							.AddInParameter("@SysKey", sysKey, DbType.AnsiString, 100)
							.ExecuteReader(tm).MapToSingle(new SysConfigEO().GetMapper());
		}


		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "sysKey">参数名称</param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<SysConfigEO> SelectBySysKey(string sysKey, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [SysKey], [SysValue], [SysType] FROM [dbo].[SysConfig] WHERE [SysKey]=@SysKey");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter("@SysKey", sysKey, DbType.AnsiString, 100)
							.ExecuteList(new SysConfigEO().GetMapper(), tm);
		}
		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "sysValue">参数值</param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<SysConfigEO> SelectBySysValue(string sysValue, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [SysKey], [SysValue], [SysType] FROM [dbo].[SysConfig] WHERE [SysValue]=@SysValue");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter("@SysValue", sysValue, DbType.String, -1)
							.ExecuteList(new SysConfigEO().GetMapper(), tm);
		}
		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "sysType">
		/// 参数类型
		///    Sys-1-系统参数
		/// </param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<SysConfigEO> SelectBySysType(SysConfigEO.SysTypeEnum sysType, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [SysKey], [SysValue], [SysType] FROM [dbo].[SysConfig] WHERE [SysType]=@SysType");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter("@SysType", sysType, DbType.Byte, 1)
							.ExecuteList(new SysConfigEO().GetMapper(), tm);
		}

		/// <summary>
		/// 按自定义条件查询
		/// </summary>
		/// <param name = "where">自定义条件,where子句</param>
		/// <param name = "top">获取行数</param>
		/// <param name = "sort">排序表达式</param>
		/// <param name = "paras">where子句中定义的参数集合</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<SysConfigEO> Select(string where, int top, string sort, IEnumerable<DbParameter> paras, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [SysKey], [SysValue], [SysType] FROM [dbo].[SysConfig] ");
			if (!string.IsNullOrEmpty(where)) format.AppendFormat(" WHERE {0} ", where);
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddParameters(paras)
							.ExecuteList(new SysConfigEO().GetMapper(), tm);
		}

		/// <summary>
		/// 分页查询
		/// </summary>
		/// <param name = "where">条件表达式</param>
		/// <param name = "sort">排序表达式</param>
		/// <param name = "paras">传入参数</param>
		/// <param name = "pageSize">页大小</param>
		/// <param name = "pageIndex">页码</param>
		/// <param name = "recordCount">总记录数，由于计算总记录数影响数据库性能，只有小于或等于0的情况下才返回总记录数</param>
		/// <return>分页数据</return>
		public List<SysConfigEO> SelectPager(string where, string sort, IList<DbParameter> paras, long pageSize, long pageIndex, ref long recordCount)
		{
			var sql = new StringBuilder();
			sql.AppendFormat(@"SELECT [SysKey], [SysValue], [SysType] FROM [dbo].[SysConfig]");
			if (!string.IsNullOrEmpty(where)) sql.AppendFormat(" WHERE {0} ", where);
			if (!string.IsNullOrEmpty(sort)) sql.AppendFormat(" ORDER BY {0} ", sort);
			var pager = this.DataBase.CreatePager(sql.ToString(), pageSize, "SysKey");
			pager.AddParameters(paras);
			if (recordCount <= 0)
			{
				recordCount = pager.GetRecordCount();
			}
			return pager.GetPageData(pageIndex).MapToList(new SysConfigEO().GetMapper());
		}

		#endregion

		#region Insert方法集合
		/// <summary>
		/// 插入数据，有自增列时，item中自增字段为新增值，否则，返回影响的行数
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "item">要插入的实体对象</param>
		/// <return></return>
		public int Insert(SysConfigEO item, TransactionManager tm = null)
		{
			const string sql = @"INSERT INTO [dbo].[SysConfig] ([SysKey], [SysValue], [SysType]) VALUES (@SysKey, @SysValue, @SysType) ";
			return AdoBase(sql)
												.AddInParameter("@SysKey", item.SysKey, DbType.AnsiString, 100)
												.AddInParameter("@SysValue", item.SysValue != null ?  (object)item.SysValue : DBNull.Value, DbType.String, -1)
												.AddInParameter("@SysType", item.SysType, DbType.Byte, 1)
												.ExecuteNonQuery(tm);
		}
		#endregion

		#region Update方法集合
		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "item">要更新的实体对象</param>
		/// <param name = "tm">事务管理对象</param>
		/// <return></return>
		public int Update(SysConfigEO item, TransactionManager tm = null)
		{
		if (item.SysKey == null) throw new ArgumentNullException("属性SysKey不能为null.");
			const string sql = @"UPDATE [dbo].[SysConfig] SET [SysKey]=@SysKey, [SysValue]=@SysValue, [SysType]=@SysType WHERE [SysKey]=@OriginalSysKey";
			return AdoBase(sql)
							.AddInParameter("@OriginalSysKey", (item.HasOriginal) ? item.OriginalSysKey : item.SysKey, DbType.AnsiString, 100)
							.AddInParameter("@SysKey", item.SysKey, DbType.AnsiString, 100)
							.AddInParameter("@SysValue", item.SysValue != null ?  (object)item.SysValue : DBNull.Value, DbType.String, -1)
							.AddInParameter("@SysType", item.SysType, DbType.Byte, 1)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "sysKey">参数名称</param>
		/// <param name = "sysValue">参数值</param>
		/// <return></return>
		public virtual int UpdateSysValue(string sysKey, string sysValue)
		{
			return UpdateSysValue(sysKey, sysValue, null);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "tm">事务管理对象</param>
		/// <param name = "sysKey">参数名称</param>
		/// <param name = "sysValue">参数值</param>
		/// <return></return>
		public virtual int UpdateSysValue(string sysKey, string sysValue, TransactionManager tm = null)
		{
			const string sql = @"UPDATE [dbo].[SysConfig] SET [SysValue]=@SysValue  WHERE [SysKey]=@SysKey";
			return AdoBase(sql)
							.AddInParameter("@SysValue", sysValue != null ?  (object)sysValue : DBNull.Value, DbType.String, -1)
							.AddInParameter("@SysKey", sysKey, DbType.AnsiString, 100)
							.ExecuteNonQuery(tm);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "sysKey">参数名称</param>
		/// <param name = "sysType">
		/// 参数类型
		///    Sys-1-系统参数
		/// </param>
		/// <return></return>
		public virtual int UpdateSysType(string sysKey, SysConfigEO.SysTypeEnum sysType)
		{
			return UpdateSysType(sysKey, sysType, null);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "tm">事务管理对象</param>
		/// <param name = "sysKey">参数名称</param>
		/// <param name = "sysType">
		/// 参数类型
		///    Sys-1-系统参数
		/// </param>
		/// <return></return>
		public virtual int UpdateSysType(string sysKey, SysConfigEO.SysTypeEnum sysType, TransactionManager tm = null)
		{
			const string sql = @"UPDATE [dbo].[SysConfig] SET [SysType]=@SysType  WHERE [SysKey]=@SysKey";
			return AdoBase(sql)
							.AddInParameter("@SysType", sysType, DbType.Byte, 1)
							.AddInParameter("@SysKey", sysKey, DbType.AnsiString, 100)
							.ExecuteNonQuery(tm);
		}

		/// <summary>
		/// 按自定义条件更新数据
		/// </summary>
		/// <param name = "set">更新列</param>
		/// <param name = "where">更新条件</param>
		/// <param name = "paras">参数集合</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public int Update(string set, string where, IEnumerable<DbParameter> paras, TransactionManager tm = null)
		{
			if (string.IsNullOrEmpty(set)) throw new ArgumentNullException("set");
			var sql = string.Format(@"UPDATE [dbo].[SysConfig] SET {0} {1}", set, (string.IsNullOrEmpty(where) ? string.Empty : " WHERE " + where));
			return AdoBase(sql)
							.AddParameters(paras)
							.ExecuteNonQuery(tm);
		}
		#endregion

		#region Delete方法集合
		/// <summary>
		/// 按主键删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "sysKey">参数名称</param>
		public int DeleteByPK(string sysKey, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM [dbo].[SysConfig] WHERE [SysKey]=@SysKey";
			return AdoBase(sql)
							.AddInParameter("@SysKey", sysKey, DbType.AnsiString, 100)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 按字段删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "sysValue">参数值</param>
		public int DeleteBySysValue(string sysValue, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM [dbo].[SysConfig] WHERE [SysValue]=@SysValue";
			return AdoBase(sql)
							.AddInParameter("@SysValue", sysValue != null ?  (object)sysValue : DBNull.Value, DbType.String, -1)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 按字段删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "sysType">
		/// 参数类型
		///    Sys-1-系统参数
		/// </param>
		public int DeleteBySysType(SysConfigEO.SysTypeEnum sysType, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM [dbo].[SysConfig] WHERE [SysType]=@SysType";
			return AdoBase(sql)
							.AddInParameter("@SysType", sysType, DbType.Byte, 1)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 删除指定条件的记录
		/// </summary>
		/// <param name = "whereClause">自定义删除条件，where子句。如：id=1 and name='aaa'</param>
		/// <param name = "paras">where子句中定义的参数集合</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public int Delete(string whereClause, IEnumerable<DbParameter> paras, TransactionManager tm = null)
		{
			var sql = string.Format(@"DELETE FROM [dbo].[SysConfig] {0}", (string.IsNullOrEmpty(whereClause) ? string.Empty : " WHERE " + whereClause));
			return AdoBase(sql)
							.AddParameters(paras)
							.ExecuteNonQuery(tm);
		}
		#endregion

		#region  自定义方法集合
		#endregion

	}


	#endregion
}
